// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.directives','app.services',])

.config(function($ionicConfigProvider, $sceDelegateProvider){
  

  $sceDelegateProvider.resourceUrlWhitelist([ 'self','*://www.youtube.com/**', '*://player.vimeo.com/video/**']);

})
    .config(["$httpProvider",
        function ($httpProvider) {
            // @ifdef DEBUG
            // $httpProvider.defaults.headers.post['Authorization'] = "Basic " + btoa("test:test");
            // @endif
            $httpProvider.defaults.headers.post['Content-Type'] = "application/x-www-form-urlencoded";
            $httpProvider.defaults.transformRequest = function (data) {
                if (data === undefined) {
                    return data;
                }

                var $filter = angular.injector(['ng']).get('$filter');

                //把日期格式化为后台可以接收的格式
                var convertDate = function (para) {
                    for (var prop in para) {
                        var value = para[prop];

                        if (value instanceof Date) {
                            para[prop] = $filter('date')(value, 'yyyy-MM-dd')
                        }

                        //notice: 有可能有循环引用
                        value && typeof(value) == 'object' && convertDate(value);
                    }
                };
                convertDate(data);

                return data instanceof FormData ? data : angular.injector(['ng']).get('$httpParamSerializer')(data);
            }

            // $httpProvider.interceptors.push(function ($q, $rootScope, $window) {
            //     return {
            //         'request': function (config) {
            //             return config;
            //         },
            //
            //         'response': function (response) {
            //             return response;
            //         },
            //
            //         'responseError': function (rejection) {
            //             if (rejection.status == '401') {
            //                 $window.location.href = "/c/portal/login?redirect=" + $window.location.pathname + $window.location.hash.replace('#', '%23');
            //
            //             }
            //
            //             return $q.reject(rejection);
            //         }
            //     }
            // })
        }
    ])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})

/*
  This directive is used to disable the "drag to open" functionality of the Side-Menu
  when you are dragging a Slider component.
*/
.directive('disableSideMenuDrag', ['$ionicSideMenuDelegate', '$rootScope', function($ionicSideMenuDelegate, $rootScope) {
    return {
        restrict: "A",  
        controller: ['$scope', '$element', '$attrs', function ($scope, $element, $attrs) {

            function stopDrag(){
              $ionicSideMenuDelegate.canDragContent(false);
            }

            function allowDrag(){
              $ionicSideMenuDelegate.canDragContent(true);
            }

            $rootScope.$on('$ionicSlides.slideChangeEnd', allowDrag);
            $element.on('touchstart', stopDrag);
            $element.on('touchend', allowDrag);
            $element.on('mousedown', stopDrag);
            $element.on('mouseup', allowDrag);

        }]
    };
}])

/*
  This directive is used to open regular and dynamic href links inside of inappbrowser.
*/
.directive('hrefInappbrowser', function() {
  return {
    restrict: 'A',
    replace: false,
    transclude: false,
    link: function(scope, element, attrs) {
      var href = attrs['hrefInappbrowser'];

      attrs.$observe('hrefInappbrowser', function(val){
        href = val;
      });
      
      element.bind('click', function (event) {

        window.open(href, '_system', 'location=yes');

        event.preventDefault();
        event.stopPropagation();

      });
    }
  };
});